package com.sangmo.fts.api.factoring;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.factoring.interfaces.FfFactoringContractCreation;
import com.sangmo.fts.process.factoring.model.FfFactoringContract;
import com.sangmo.fts.process.factoring.model.FfLoan;
import com.sangmo.fts.process.factoring.service.FfFactoringContractService;
import com.sangmo.fts.process.receivable.interfaces.RpfPledgeContractCreation;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/ffFactoringContract/v1")
@Access("ffFactoringContract")
public class FfFactoringContractApi {

    @Autowired
    private FfFactoringContractService ffFactoringContractService;

    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<FfFactoringContract> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid FfFactoringContractCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(ffFactoringContractService.create(obj));
    }

    @Access(":remove")
    @PostMapping("/{trainingId}/{id}/remove")
    public @ResponseBody ApiResponse remove(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffFactoringContractService.deleteById(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<FfFactoringContract>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("FfFactoringContract.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ffFactoringContractService.search(ps));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/read")
    public @ResponseBody ApiResponse<FfFactoringContract> read(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ExtViewHelper.setGroup("FfFactoringContract.detail", 1);
        return ApiResponse.make(ffFactoringContractService.find(id));
    }

    @TrainingProcess(node = TrainingHistory.FfProcessNodes.FF_FACTORING_CONTRACT_BANK_CONFIRM_7)
    @Access(":bankConfirm")
    @PostMapping({"/{trainingId}/{id}/bank/confirm"})
    @ResponseBody
    public ApiResponse bankConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffFactoringContractService.bankConfirm(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.FfProcessNodes.FF_FACTORING_CONTRACT_SELLER_CONFIRM_8)
    @Access(":sellerConfirm")
    @PostMapping({"/{trainingId}/{id}/seller/confirm"})
    @ResponseBody
    public ApiResponse sellerConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffFactoringContractService.sellerConfirm(id);
        return ApiResponse.make();
    }

    @Access(":selection")
    @PostMapping("/{trainingId}/selection")
    public @ResponseBody ApiResponse<PageResult<FfFactoringContract>> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("FfFactoringContract.selection", 2);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ffFactoringContractService.selection(ps));
    }


}
